package com.sunshine.schedulegrid.adapter

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.viewpager.widget.PagerAdapter
import com.sunshine.schedulegrid.config.ScheduleConfig
import com.sunshine.schedulegrid.utils.ColorUtils
import com.sunshine.schedulegrid.utils.CourseList
import com.sunshine.schedulegrid.utils.DateUtils
import com.sunshine.schedulegrid.view.DateView
import com.sunshine.schedulegrid.view.MainView

/**
 * @author sunshine
 * @date 2020/9/2
 */
class WeekSlideViewPagerAdapter(
    private val scheduleConfig: ScheduleConfig,
    private val courseList: CourseList,
    private val colorUtils: ColorUtils
) : PagerAdapter() {

    override fun isViewFromObject(view: View, `object`: Any): Boolean {
        return view === `object`
    }

    override fun getCount(): Int {
        return scheduleConfig.weekCount()
    }

    override fun instantiateItem(container: ViewGroup, position: Int): Any {
        val root = LinearLayout(container.context).apply { orientation = LinearLayout.VERTICAL }

        //这周的时间 = 开始时间+第几页（第几周）
        val dateView = DateView(
            context = container.context,
            sc = scheduleConfig,
            thisDateLong = scheduleConfig.startDate() + position * DateUtils.aWeekLong
        )
        val mainView = MainView(
            context = container.context,
            sc = scheduleConfig,
            cl = courseList,
            thisDateLong = scheduleConfig.startDate() + position * DateUtils.aWeekLong,
            cu = colorUtils
        )

        //加到root界面
        root.apply {
            addView(dateView, LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                scheduleConfig.dateHeight()
            ))
            addView(mainView, LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT
            ))
        }

        container.addView(root)
        return root
    }

    override fun getItemPosition(`object`: Any): Int {
        return POSITION_NONE
    }

    override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
        val view = `object` as LinearLayout
        container.removeView(view)
    }
}